package com.okyai.kt.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.okyai.common.annotation.Log;
import com.okyai.common.enums.BusinessType;
import com.okyai.kt.domain.Demography;
import com.okyai.kt.service.IDemographyService;
import com.okyai.common.core.controller.BaseController;
import com.okyai.common.core.domain.AjaxResult;
import com.okyai.common.utils.poi.ExcelUtil;
import com.okyai.common.core.page.TableDataInfo;

/**
 * 人口学Controller
 * 
 * @author okyai
 * @date 2021-03-20
 */
@Controller
@RequestMapping("/kt/demography")
public class DemographyController extends BaseController
{
    private String prefix = "kt/demography";

    @Autowired
    private IDemographyService demographyService;

    @RequiresPermissions("kt:demography:view")
    @GetMapping()
    public String demography()
    {
        return prefix + "/demography";
    }

    /**
     * 查询人口学列表
     */
    @RequiresPermissions("kt:demography:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Demography demography)
    {
        startPage();
        List<Demography> list = demographyService.selectDemographyList(demography);
        return getDataTable(list);
    }

    /**
     * 导出人口学列表
     */
    @RequiresPermissions("kt:demography:export")
    @Log(title = "人口学", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Demography demography)
    {
        List<Demography> list = demographyService.selectDemographyList(demography);
        ExcelUtil<Demography> util = new ExcelUtil<Demography>(Demography.class);
        return util.exportExcel(list, "demography");
    }

    /**
     * 新增人口学
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存人口学
     */
    @RequiresPermissions("kt:demography:add")
    @Log(title = "人口学", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Demography demography)
    {
        return toAjax(demographyService.insertDemography(demography));
    }

    /**
     * 修改人口学
     */
    @GetMapping("/edit/{demographyId}")
    public String edit(@PathVariable("demographyId") Long demographyId, ModelMap mmap)
    {
        Demography demography = demographyService.selectDemographyById(demographyId);
        mmap.put("demography", demography);
        return prefix + "/edit";
    }

    /**
     * 修改保存人口学
     */
    @RequiresPermissions("kt:demography:edit")
    @Log(title = "人口学", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Demography demography)
    {
        return toAjax(demographyService.updateDemography(demography));
    }

    /**
     * 删除人口学
     */
    @RequiresPermissions("kt:demography:remove")
    @Log(title = "人口学", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(demographyService.deleteDemographyByIds(ids));
    }
}
